**************************************************************
*** SHP: Combining individual, household and parental data ***

*** SHP: combining all waves

**************************************************************

* Note: save all waves in one folder first

cd "SET_YOUR_PATH\SHP\Data STATA\SHP-Data-W1-W20-STATA\all_waves\"

*** Merging individual and household data

foreach w in 99 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
		
	use shp`w'_p_user if status`w'==0, clear
	merge m:1 idhous`w' using shp`w'_h_user, keep (match master)

	isvar 	idpers idhous`w' status`w' 																	/// identifier
			nbpers`w' h`w'i30 i`w'htyg i`w'htyn i`w'eqsg i`w'eqsn i`w'eqog i`w'eqon i`w'dispy			/// 
			p`w'p01 p`w'p02 p`w'p03 p`w'p04 p`w'p06 p`w'p10 p`w'p11 p`w'p19 p`w'p26 p`w'p66 p`w'p67		/// 
			i`w'ptotn i`w'ptotg i`w'wyg i`w'wyn x`w'i04	p`w'w92 p`w'i01 p`w'i02							/// 
			p`w'c44 p`w'c02 p`w'f01 p`w'f02 p`w'ql04 p`w'a05 p`w'a06 									/// 
			p`w'c47 p`w'c48 p`w'c49 p`w'c50																/// 
			p`w'p87 p`w'p88 p`w'p89 p`w'p90 p`w'p91 p`w'p92 p`w'p93 									/// 
			p`w'c140 p`w'c141 p`w'c142 p`w'c143 p`w'c144 p`w'c145 p`w'c146 p`w'c147 p`w'c148 			/// 
			p`w'c149 p`w'c150 p`w'c151 p`w'c152 p`w'c153 p`w'c154 										/// 
			age`w' sex`w' wstat`w'	isced`w' hab_ch`i'													// 			
	local OKlist "`r(varlist)'"
	keep `OKlist'
	
	gen syear`w' = 2000+`w'
	replace syear`w' = 1999 if syear`w'==2099

	save pers, replace

	use pers, clear


	*** Merging individual and parental data
	merge 1:1 idpers using "F:\Uni\Daten\Household Panels\SHP\Data STATA\SHP-Data-WA-STATA\shp_mp.dta", keepusing (idfath__ idmoth__) ///
						keep (match master) nogen // we do not want to add other individ.
	save temp_parents, replace

	*Second, we create a temporary file containing the variables of the mother. We also drop the variable idfath__ because it is not needed.
	use temp_parents, clear
	drop idmoth__ idfath__
	rename idpers idmoth__
	isvar p`w'p01 p`w'p02 p`w'p03 p`w'p04 p`w'p06 p`w'p10 p`w'p11 p`w'p19 p`w'p26 p`w'p66 p`w'p67 h`w'i30 i`w'htyn p`w'i01 nbpers`w' isced`w'
	local OKlist2 "`r(varlist)'"
	foreach var of varlist `OKlist2' {
		rename `var' m_`var'
	}
	save temp_mother, replace

	*Third, we create a temporary file containing the variables of the father (same as for mother).
	use temp_parents, clear
	drop idfath__ idmoth__
	rename idpers idfath__
	foreach var of varlist `OKlist2' {
		rename `var' f_`var'
	}
	save temp_father, replace

	*Fourth, we merge the variables of the parents to the individual file
	use temp_parents, clear
	merge m:1 idmoth__ using temp_mother, keep (match master) gen(merge_mom)
	merge m:1 idfath__ using temp_father, keep (match master) gen(merge_dad)

	save shp`w'_final, replace

	erase pers.dta 
	erase temp_mother.dta 
	erase temp_father.dta 
}





**********************
*** Combine waves ****
**********************

*First step: create a wide data set 
use shp99_final, clear

foreach w in 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 {
	merge 1:1 idpers using shp`w'_final, nogen 
}

save shp_wide, replace
use shp_wide, clear


* recode migration status
fre hab_ch*
foreach var of varlist hab_ch* {
	recode `var' (-5 -3=0 "no migrant")(-1/2020=1 "migrant")(else=.), gen(`var'_r)
}
fre hab_ch99_r

gen migrant = .
replace migrant = hab_ch99_r
replace migrant = hab_ch00_r if migrant==.
replace migrant = hab_ch01_r if migrant==.
replace migrant = hab_ch02_r if migrant==.
replace migrant = hab_ch03_r if migrant==.
replace migrant = hab_ch04_r if migrant==.
replace migrant = hab_ch05_r if migrant==.
replace migrant = hab_ch06_r if migrant==.
replace migrant = hab_ch07_r if migrant==.
replace migrant = hab_ch08_r if migrant==.
replace migrant = hab_ch09_r if migrant==.
replace migrant = hab_ch10_r if migrant==.
replace migrant = hab_ch11_r if migrant==.
replace migrant = hab_ch12_r if migrant==.
replace migrant = hab_ch13_r if migrant==.
replace migrant = hab_ch14_r if migrant==.
replace migrant = hab_ch15_r if migrant==.
replace migrant = hab_ch16_r if migrant==.
replace migrant = hab_ch17_r if migrant==.
replace migrant = hab_ch18_r if migrant==.

drop hab_ch*


*Second step: reshape to the long format
reshape long idhous@ status@ f_nbpers@ m_nbpers@																/// identifier
			nbpers@ h@i30 m_h@i30 f_h@i30 i@htyg i@htyn i@eqsg i@eqsn i@eqog i@eqon i@dispy	f_i@htyn m_i@htyn	/// 
			p@p01 p@p02 p@p03 p@p04 p@p06 p@p10 p@p11 p@p19 p@p26 p@p66 p@p67									/// 
			m_p@p01 m_p@p02 m_p@p03 m_p@p04 m_p@p06 m_p@p10 m_p@p11 m_p@p19 m_p@p26 m_p@p66 m_p@p67				/// 
			f_p@p01 f_p@p02 f_p@p03 f_p@p04 f_p@p06 f_p@p10 f_p@p11 f_p@p19 f_p@p26 f_p@p66 f_p@p67				/// 
			i@ptotn i@ptotg i@wyg i@wyn x@i04 p@w92 p@i01 m_p@i01 f_p@i01 p@i02									/// 
			p@c44 p@c02 p@f01 p@f02 p@ql04 p@a05 p@a06 															/// 
			p@c47 p@c48 p@c49 p@c50																				/// 
			p@p87 p@p88 p@p89 p@p90 p@p91 p@p92 p@p93 															/// 
			p@c140 p@c141 p@c142 p@c143 p@c144 p@c145 p@c146 p@c147 p@c148 										/// 
			p@c149 p@c150 p@c151 p@c152 p@c153 p@c154 															/// 
			age@ sex@ wstat@ isced@ m_isced@ f_isced@ , 														/// 
		i(idpers) j(year "99" "00" "01" "02" "03" "04" "05" "06" "07" "08" 										///
							"09" "10" "11" "12" "13" "14" "15" "16" "17" "18") atwl ()

recode year (0=2000)(1=2001)(2=2002)(3=2003)(4=2004)(5=2005)(6=2006)(7=2007) ///
		(8=2008)(9=2009)(10=2010)(11=2011)(12=2012)(13=2013)(14=2014)(15=2015) ///
		(16=2016)(17=2017)(18=2018)(99=1999)

drop m_pp01-f_pp67 pc44-pc154 m_isced f_isced f_ihtyn m_ihtyn f_nbpers m_nbpers		
		
save shp_long, replace

**********************
*** Recode data ******
**********************

use shp_long, replace

lab var idhous "Identifier number of Household"
lab var year "Year"


* Household-level data
lab var nbpers "Number of persons in HH"
lab var hi30 "Satisfaction with financial situation of HH"
lab var ihtyg "Yearly Household Income, gross"
lab var ihtyn "Yearly Household Income, net"
lab var ieqsg "Yearly household income equivalised, SKOS, gross"
lab var ieqsn "Yearly household income equivalised, SKOS, net"
lab var ieqog "Yearly household income equivalised, OECD, gross"
lab var ieqon "Yearly household income equivalised, OECD, net"
lab var idispy "Disposable household income: yearly amount"

*** Individual level

* vote and political participation
lab var pp01 "Interest in politics"
lab var pp02 "Overall satisfaction with democracy"
lab var pp03 "Feeling about political influence"
lab var pp04 "Trust in Federal Government"
lab var pp06 "Participation in federal polls"
lab var pp10 "Political position: Left, right"
lab var pp11 "Member of a political party: Which"
lab var pp19 "Party choice in case of elections tomorrow"
lab var pp26 "Trust in political parties"
lab var pp66 "Party identification: yes-no"
lab var pp67 "Party identification: Party"


* financial situation

lab var iptotn "Yearly total personal income, net"
lab var iptotg "Yearly total personal income, gross"
lab var iwyg "Yearly work income, gross"
lab var iwyn "Yearly work income, gross"
lab var xi04 "Total personal income per year"
lab var pw92 "CMJ: Satisfaction: Income"
lab var pi01 "Satisfaction with financial situation"
lab var pi02 "Satisfaction with financial situation: Change"


*Socio-demographics
lab var age "Age"
lab var sex "Sex"
lab var wstat "Employment status"
lab var isced "International Standard Classification of Education ISCED 1997"






***********
** Recode for analyses
***********

*** recode variables
mvdecode ihtyn hi30 m_hi30 f_hi30 pi01 m_pi01 f_pi01 pi02 pp01 pp02 pp03 pp06 pp04 pp26 ///
	pp66 nbpers age sex wstat isced, mv(-8/-1)

*sep
clonevar hhsize = nbpers
gen hhinc = ihtyn
gen hhinc_pp = ihtyn/nbpers
gen hhinc_pp_sqrt = ihtyn/(sqrt(nbpers))

xtile hhinc_dec = hhinc, nq(10) 
xtile hhinc_dec_pp = hhinc_pp, nq(10) 
xtile hhinc_dec_pp_sqrt = hhinc_pp_sqrt, nq(10) 

clonevar hhinc_sat = hi30
clonevar inc_sat = pi01
clonevar inc_change_sat = pi02

gen hhinc_dec10 = (hhinc_dec-1)*(10/9)
gen hhinc_dec_pp10 = (hhinc_dec_pp-1)*(10/9)
gen hhinc_dec_pp_sqrt10 = (hhinc_dec_pp_sqrt-1)*(10/9)


*political variables 
recode pp19 (-2 -1 52=0 "would not vote") (1/51=1 "would vote")(else=.), gen(vote) // (CHECK CODING!)
clonevar polint = pp01
clonevar satdem = pp02
clonevar exeff = pp03
clonevar polls_times = pp06
clonevar trust_gov = pp04
clonevar trust_party = pp26
clonevar partyid = pp66

fre polint satdem exeff polls_times trust_gov trust_party partyid


*socio-demographics
gen female2 = sex-1
replace female2 = 0 if idpers==22289101
by idpers: egen female = max(female2)

drop female2

recode wstat (2=1 "unemployed")(1 3=0 "other")(else=.), gen(unemployed)
recode wstat (3=1 "not in labor force")(1 2=0 "other")(else=.), gen(nlf)
recode isced (0/20=1 "low")(31/60=0 "other")(else=.), gen(edu_low)
recode isced (31/33=1 "medium")(0/20 41/60=0 "other")(else=.), gen(edu_med)
recode isced (41/60=1 "high")(0/40=0 "other")(else=.), gen(edu_high)

recode age (13/25=1)(26/120=0)(else=.), gen(age1825)
recode age (13/35=1)(36/120=0)(else=.), gen(age1835)
gen age18 = age-18
center age, gen(age_c)




*save long
save shp_long, replace


